package com.itheima.controller.backend.security;

import com.itheima.common.constant.MessageConstant;
import com.itheima.mapper.UserMapper;
import com.itheima.common.pojo.User;
import com.itheima.common.pojo.Role;
import com.itheima.common.pojo.Permission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * 实现自定义 Spring Security 的用户认证逻辑
 */

@Service
public class SpringSecurityUserService implements UserDetailsService {

    @Autowired
    private UserMapper userMapper;

    //根据用户名查询数据库中用户信息
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

//        //1. 根据用户名查询用户基本信息
//        User user = userMapper.findByUsername(username);
//        if (user == null){
//            throw new UsernameNotFoundException(MessageConstant.GET_USERNAME_FAIL);
//        }
//
//        //2. 创建权限列表
//        List<SimpleGrantedAuthority> authoryList = new ArrayList<>();
//
//        //3.获取用户角色集合
//        Set<Role> roles = user.getRoles();
//        if (roles != null && roles.size() > 0){
//            for (Role role : roles) {
//                //添加角色
//                authoryList.add(new SimpleGrantedAuthority("ROLE_" + role.getKeyword()));
//                //获取角色对应的权限
//                Set<Permission> permissions = role.getPermissions();
//                if (permissions != null && permissions.size() > 0){
//                    for (Permission permission : permissions) {
//                        //添加权限
//                        authoryList.add(new SimpleGrantedAuthority(permission.getKeyword()));
//                    }
//                }
//            }
//        }
//        //4.返回UserDetails对象
//        return new org.springframework.security.core.userdetails.User(
//                user.getUsername(),
//                user.getPassword(),
//                authoryList
//        );


        // 1. 查询用户基本信息
        User user = userMapper.findByUsername(username);
        if (user == null) {
            throw new UsernameNotFoundException(MessageConstant.GET_USERNAME_FAIL);
        }

        // 2. 创建权限列表
        List<SimpleGrantedAuthority> authorities = new ArrayList<>();

        // 3. 获取用户角色集合
        Set<Role> roles = user.getRoles();
        if (roles != null && roles.size() > 0) {
            for (Role role : roles) {
                // 添加角色
                authorities.add(new SimpleGrantedAuthority("ROLE_" + role.getKeyword()));

                // 获取角色对应的权限
                Set<Permission> permissions = role.getPermissions();
                if (permissions != null && permissions.size() > 0) {
                    for (Permission permission : permissions) {
                        // 添加权限
                        authorities.add(new SimpleGrantedAuthority(permission.getKeyword()));
                    }
                }
            }
        }

        // 4. 返回UserDetails对象
        return new org.springframework.security.core.userdetails.User(
            user.getUsername(),
            user.getPassword(),
            authorities
        );
    }
}

